package com.example.Lezedata.ui.annPage;

import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.viewpager2.widget.ViewPager2;
import com.example.Lezedata.R;
import com.example.Lezedata.ui.fragment.BaseFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;

import bid.SystemAnnouncement;

public class AnnPageFragment extends BaseFragment {

    private AnnPageViewModel mViewModel;
    ArrayList<Fragment> fragments = new ArrayList<>();
    private ArrayList<String> titleList = new ArrayList<>();

    public static AnnPageFragment newInstance() {
        return new AnnPageFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view= inflater.inflate(R.layout.tab_ann_page_fragment, container, false);
        createPageView(view);
        return view ;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(AnnPageViewModel.class);
        // TODO: Use the ViewModel
    }

    private void createPageView(View view){
        TabLayout tab = (TabLayout)  view.findViewById(R.id.tab);
        ViewPager2 vp2 = (ViewPager2) view.findViewById(R.id.vp2);

        //设置标题
        titleList.add(this.getString(R.string.ann_annoucement));
        titleList.add(this.getString(R.string.ann_news));
        titleList.add(this.getString(R.string.ann_notice));
        titleList.add(this.getString(R.string.ann_active));
        SystemAnnouncement.AnnounceType[]types=new SystemAnnouncement.AnnounceType[]{
                SystemAnnouncement.AnnounceType.ANCT_ANNOUNCE,
                SystemAnnouncement.AnnounceType.ANCT_NEWS,
                SystemAnnouncement.AnnounceType.ANCT_NOTICE,
                SystemAnnouncement.AnnounceType.ANCT_ACTIVE};
        for(int i=0;i<titleList.size();i++) {
            AnnListFragment fragment = new AnnListFragment( );
            fragment.setAnnounceType(types[i]);
            fragments.add(fragment);

        }

//        vp2.setRotation(-90);
        //设置上下滑动
        vp2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);

        //定义适配器
        //设置自定义的适配器必需继承FragmentStateAdapter
        AnnPageAdapter myAdapter = new AnnPageAdapter(this.getActivity(), fragments, titleList);
        vp2.setAdapter(myAdapter);
        new TabLayoutMediator(tab, vp2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(titleList.get(position));
            }
        }).attach();
    }

}